package edu.utdallas_elearn_calendar.utdallas_elearn_calendar_app;

import edu.utdallas_elearn_calendar.utdallas_elearn_calendar_db.CalDbAdapter;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
//import android.widget.Toast;
import android.widget.SimpleCursorAdapter;

public class event_type_edit extends Activity {
	private EditText mEventTypeNameText;
	private Spinner mEventPriorityIdSpin;
	private Long mRowId;
	private CalDbAdapter mDbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		mDbHelper = new CalDbAdapter(this);
		mDbHelper.open();
        setContentView(R.layout.event_type_edit);
		  
        mEventTypeNameText = (EditText) findViewById(R.id.event_type_edit_name);
        mEventPriorityIdSpin = (Spinner) findViewById(R.id.event_type_edit_value);

		Button saveButton = (Button) findViewById(R.id.event_type_edit_button);
		mRowId = null;
		Bundle extras = getIntent().getExtras();
		mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
				.getSerializable(CalDbAdapter.ROW_ID);
		
		if (extras != null) {
			mRowId = extras.getLong(CalDbAdapter.ROW_ID);
		}
		
//		Toast.makeText(this, "The id number of this event record is " + mRowId + ".", Toast.LENGTH_LONG).show();
		
		populateFields();
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				setResult(RESULT_OK);
				finish();
			}
		});
    }
    
	private void populateFields() {
		int index = 0;
		long priorityId = 1;
		
		if (mRowId != null && mRowId != 0) {
			Cursor currentRow = mDbHelper.getEventType(mRowId);
			startManagingCursor(currentRow);
			
			mEventTypeNameText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.EVENT_TYPE)));
			
			priorityId = currentRow.getLong(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.EVENT_PRIORITY));
		
			currentRow.close();
		}
		
		index = populatePrioritySpin(priorityId);
		mEventPriorityIdSpin.setSelection(index);
	}
	
	private int populatePrioritySpin(long priorityId) {
		Cursor currentRow = mDbHelper.getAllPriorities();
		
		startManagingCursor(currentRow);
				
		// create an array to specify which fields we want to display
		String[] from = new String[]{CalDbAdapter.PRIORITY_VAL};

		// create an array of the display item we want to bind our data to
		int[] to = new int[]{android.R.id.text1};
		
		// create simple cursor adapter
		SimpleCursorAdapter adapter =
		  new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, currentRow, from, to );
		
		adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
		
		// get reference to our spinner
		Spinner s = (Spinner) findViewById( R.id.event_type_edit_value );
		s.setAdapter(adapter);
		
		int index = 0;
		int returnVal = 0;
		
		currentRow.moveToFirst();
		
		if(currentRow.getCount() > 0)
		{			
			do
			{
				if (priorityId == currentRow.getLong(currentRow.getColumnIndexOrThrow(CalDbAdapter.ROW_ID))) {
					returnVal = index;
				}
				index++;
			}
			while(currentRow.moveToNext());
		}
		else
		{
			Log.i("DATABASE", "getAllPriorities: no priorities found");
		}
		
		return returnVal;
		
	}	
	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(CalDbAdapter.ROW_ID, mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
		//mDbHelper.close();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//mDbHelper.open();
		populateFields();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mDbHelper != null) {
			mDbHelper.close();
		}
	}
	
	private void saveState() {	
		String event_type = mEventTypeNameText.getText().toString();
		long priority_id = mEventPriorityIdSpin.getSelectedItemId();

		// Insert a new or update an existing event type
		if (mRowId == null || mRowId == 0) {
			
			mDbHelper.insertEventType(priority_id, event_type);
			
		} else {

			mDbHelper.updateEventType(mRowId, priority_id, event_type);
		}
		
		mDbHelper.close();
		finish();
	}	
}	